package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.utils.BaseAction;
import cn.itcast.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Rows;
import org.apache.poi.util.SystemOutLogger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.Id;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/7/23.
 */
@Controller
@Namespace("/")
@ParentPackage("json-default")
public class FixedAction extends BaseAction<FixedArea> {
    @Autowired
    private FixedAreaService fixedAreaService;

    //添加修改的方法
    @Action(value="fixedArea_save",results = {@Result(name="success",type="redirect",location = "./pages/base/fixed_area.html")})
    public String saveFixedArea(){
        fixedAreaService.save(model);
        return SUCCESS;
    }
    //分页查询
    @Action(value = "fixedArea_pageQuery",results = {@Result(name="success",type="json")})
    public String fixedAreaPageQuery(){
        Pageable pageable = new PageRequest(page-1,rows);
        Specification<FixedArea> specification = new Specification<FixedArea>() {
            @Override
            public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if(StringUtils.isNotBlank(model.getId())){
                    Predicate p1 = cb.like(root.get("id").as(String.class),"%"+model.getId()+"%");
                    list.add(p1);
                }
                if(StringUtils.isNotBlank(model.getCompany())){
                    Predicate p2 = cb.like(root.get("company").as(String.class),"%"+model.getCompany()+"%");
                    list.add(p2);
                }

                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        Page<FixedArea> pageData = fixedAreaService.pageQuery(specification,pageable);
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }
    //查询未关联客户的方法
    @Action(value="fixArea_noassociation",results = {@Result(name="success",type = "json")})
    public String noassiocationCustomer(){
       List<Customer> noassiocationCustomers= fixedAreaService.findNoassociationCustomer();
        ActionContext.getContext().getValueStack().push(noassiocationCustomers);
        return SUCCESS;
    }
    //查询以关联客户的方法
    @Action(value="fixedArea_association",results = {@Result(name="success",type = "json")})
    public String associationCustomer(){
        String id = model.getId();
        System.out.println(id);
        List<Customer> assiocationCustomers =  fixedAreaService.findassociationCustomer(id);
        ActionContext.getContext().getValueStack().push(assiocationCustomers);
        return SUCCESS;
    }
    //关联客户与定区的方法
    //接收页面的参数
    private String [] customerIds;
    public void setCustomerIds(String[] customerIds) {this.customerIds = customerIds;}
    @Action(value = "fixedArea_associationCustomersToFixedArea",results = {@Result(name="success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public String associationCustomersToFixedArea(){
        fixedAreaService.associationCustomersToFixedArea(customerIds, model.getId());
        return SUCCESS;
    }
    //关联定区与快递员的方法
    private Integer courierId;
    private Integer takeTimeId;

    public void setCourierId(Integer courierId) {
        this.courierId = courierId;
    }

    public void setTakeTimeId(Integer takeTimeId) {
        this.takeTimeId = takeTimeId;
    }
    @Action(value = "fixedArea_associationCourierToFixedArea",results = {@Result(name="success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public  String associationCourierToFixedArea(){
        fixedAreaService.associationCourierToFixedArea(model, courierId,takeTimeId);
        return SUCCESS;
    }

}
